﻿using LeanApp.Bll;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;

namespace LeanApp.View
{
    /// <summary>
    /// QualityChartCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class QualityChartCtrl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="bll">质量分析业务</param>
        public QualityChartCtrl(string title, QualityBll bll)
        {
            InitializeComponent();
            _title = title;
            _bll = bll;
            InitialChart();
        }

        /// <summary>
        /// 初始化图表
        /// </summary>
        private void InitialChart()
        {
            title.Text = _title;
            List<ISeries> series = new List<ISeries>();
            List<Axis> xAxises = new List<Axis>();
            List<Axis> yAxises = new List<Axis>();
            if (_bll != null && _bll.RectData != null && _bll.RectData.Count() > 0)
            {
                ColumnSeries<int> item = new ColumnSeries<int>()
                {
                    Values = _bll.RectData.Values.ToArray(),
                    ScalesYAt = 0
                };
                series.Add(item);

                Axis xAxis = new Axis()
                {
                    Labels = _bll.RectData.Keys.ToArray()
                };
                xAxises.Add(xAxis);

                Axis yAxis = new Axis()
                {
                    MinLimit = 0,
                    MaxLimit = _bll.RectData.Values.Max() + 5,
                };
                yAxises.Add(yAxis);
            }

            if (_bll != null && _bll.LineData != null && _bll.LineData.Count() > 0)
            {
                LineSeries<double> item = new LineSeries<double>()
                {
                    Values = _bll.LineData.Values.ToArray(),
                    ScalesYAt = 1
                };
                series.Add(item);
                Axis yAxis = new Axis()
                {
                    MinLimit = 0,
                    MaxLimit = 105,
                    ShowSeparatorLines = false,
                    Position = LiveChartsCore.Measure.AxisPosition.End
                };
                yAxises.Add(yAxis);
            }
            chartCtrl.Series = series;
            chartCtrl.XAxes = xAxises;
            chartCtrl.YAxes = yAxises;
        }

        /// <summary>
        /// 数据实体
        /// </summary>
        private QualityBll _bll = new QualityBll();
        /// <summary>
        /// 标题
        /// </summary>
        private string _title;
    }
}
